﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using App_Code.Helpers;
using PluginDB;
using Rss;

namespace App_Code.SiteParsers
{
    /// <summary>
    /// Summary description for MenuItem
    /// </summary>
    public class MenuItem
    {
        protected NameValueCollection _queryStringParams;

        public string Name { get; set; }
        public PluginMods PluginMode { get; set; }
        public FilterTypes FilterType { get; set; }
        public string QueryString { get; set; }
        public int PageNumber { get; set; }
        public Uri Url { get; set; }

        public enum PluginMods
        {
            PLUGIN_MODE_IDLE_MODE = 0,
            PLUGIN_MODE_MOVIES,
            PLUGIN_MODE_SERIES
        }

        public enum FilterTypes
        {
            FILTER_TYPES_SEARCH,
            FILTER_TYPES_NO_FILTER,
            FILTER_TYPES_CATEGORY,
            FILTER_TYPES_MY_MOVIES,
            FILTER_TYPES_MY_SERIES,
            FILTER_TYPES_MOVIES,
            FILTER_TYPES_SERIES,
            FILTER_TYPES_LATEST,
            FILTER_TYPES_A_TO_Z_HEB,
            FILTER_TYPES_A_TO_Z_ENG,
            FILTER_TYPES_HEB_SUB,
            FILTER_TYPES_HEB_DUB,
            FILTER_TYPES_SITE,
            FILTER_TYPES_CATEGORY_ID,
            FILTER_TYPES_A_Z_HEB_ID,
            FILTER_TYPES_A_Z_ENG_ID,
            FILTER_TYPES_SERIES_ID,
            FILTER_TYPES_SEASON_NUMBER,
            FILTER_TYPES_SITE_ID,
            FILTER_TYPES_MOVIE_ID,
            FILTER_TYPES_LINK_ID
        }

        public MenuItem()
        {

        }

        public MenuItem(NameValueCollection queryStringParams)
        {
            _queryStringParams = queryStringParams;
        }

        public virtual RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = this.Name,
                Description = this.Name,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}", Utils.GetSiteUrl(), Common.PLUGIN_MODE, (int)this.PluginMode))
            };

            return rssItem;
        }
    }

    public class SearchMenuItem : MenuItem
    {
        public SearchMenuItem(PluginMods pluginMode, FilterTypes filterType)
        {
            Name = "הזן ערך לחיפוש";
            PluginMode = pluginMode;
            FilterType = filterType;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = this.Name,
                Description = this.Name,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}",
                                             Utils.GetSiteUrl(),
                                             Common.PLUGIN_MODE,
                                             (int)PluginMode,
                                             Common.FILTER_TYPE,
                                             (int)FilterType))
            };
            return rssItem;
        }
    }

    public class FilterMenuItem : MenuItem
    {
        public FilterMenuItem(string name, PluginMods pluginMode, FilterTypes filterType)
        {
            Name = name;
            PluginMode = pluginMode;
            FilterType = filterType;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = this.Name,
                Description = this.Name,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}",
                                             Utils.GetSiteUrl(),
                                             Common.PLUGIN_MODE,
                                             (int)PluginMode,
                                             Common.FILTER_TYPE,
                                             (int)FilterType))
            };
            return rssItem;
        }
    }

    public class CategoryMenuItem : MenuItem
    {
        private readonly Category _category;

        public CategoryMenuItem(Category category, PluginMods pluginMode)
        {
            this._category = category;

            Name = category.CategoryName;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_CATEGORY_ID;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = _category.CategoryName,
                Description = _category.CategoryName,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                             Utils.GetSiteUrl(),
                                             Common.PLUGIN_MODE,
                                             (int)PluginMode,
                                             Common.FILTER_TYPE,
                                             (int)FilterType,
                                             Common.CATEGORY_ID,
                                             _category.CategoryID))
            };
            return rssItem;
        }
    }

    public class NextPageMenuItem : MenuItem
    {
        public NextPageMenuItem(NameValueCollection queryStringParams, int pageNumber)
            : base(queryStringParams)
        {
            this.PageNumber = pageNumber;
        }

        public override RssItem CreateRssItem()
        {
            var newQueryString = new NameValueCollection(_queryStringParams);
            if (newQueryString[Common.PAGE_NUMBER] != null)
            {
                newQueryString[Common.PAGE_NUMBER] = (this.PageNumber + 1).ToString();
            }
            else
            {
                newQueryString.Add(Common.PAGE_NUMBER, (this.PageNumber + 1).ToString());
            }

            var rssItem = new RssItem
            {
                Title = "דף הבא",
                Description = "דף הבא",
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}", Utils.GetSiteUrl(), Utils.ConstructQueryString(newQueryString)))
            };

            return rssItem;
        }
    }

    public class MovieMenuItem : MenuItem
    {
        private readonly Video _video;

        public MovieMenuItem(Video video, PluginMods pluginMode)
        {
            _video = video;

            Name = video.VideoName;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_MOVIE_ID;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
                {
                    Title = _video.VideoName,
                    Description = _video.Plot,
                    PubDate = DateTime.Now,
                    Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                                Utils.GetSiteUrl(),
                                                Common.PLUGIN_MODE,
                                                (int)PluginMode,
                                                Common.FILTER_TYPE,
                                                (int)FilterType,
                                                Common.VIDEO_ID,
                                                _video.VideoID))
                };

            if (!string.IsNullOrEmpty(_video.ImageUrl))
            {
                rssItem.Image = new RssImage
                {
                    Url = new Uri(_video.ImageUrl),
                    Title = _video.VideoName
                };
            }
            return rssItem;
        }
    }

    public class VideoLinkMenuItem : MenuItem
    {
        private readonly VideosLink _videosLink;

        public VideoLinkMenuItem(VideosLink videosLink, PluginMods pluginMode)
        {
            _videosLink = videosLink;

            Name = _videosLink.LinkURL;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_LINK_ID;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = string.Format("{0} [{1}]", _videosLink.Video.VideoName, _videosLink.Source.SourceName),
                Description = _videosLink.Video.Plot,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                            Utils.GetSiteUrl(),
                                            Common.PLUGIN_MODE,
                                            (int)PluginMode,
                                            Common.FILTER_TYPE,
                                            (int)FilterType,
                                            Common.LINK_ID,
                                            _videosLink.LinkID))
            };

            if (!string.IsNullOrEmpty(_videosLink.Video.ImageUrl))
            {
                rssItem.Image = new RssImage
                {
                    Url = new Uri(_videosLink.Video.ImageUrl),
                    Title = _videosLink.Video.VideoName
                };
            }
            return rssItem;
        }
    }

    public class TVShowLinkMenuItem : MenuItem
    {
        private readonly TVShowsLink _tvShowsLink;

        public TVShowLinkMenuItem(TVShowsLink tvShowsLink, PluginMods pluginMode)
        {
            _tvShowsLink = tvShowsLink;

            Name = _tvShowsLink.LinkURL;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_LINK_ID;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = string.Format("{0} [{1}]", _tvShowsLink.TVShow.Name, _tvShowsLink.Source.SourceName),
                Description = _tvShowsLink.TVShow.Series.Plot,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                            Utils.GetSiteUrl(),
                                            Common.PLUGIN_MODE,
                                            (int)PluginMode,
                                            Common.FILTER_TYPE,
                                            (int)FilterType,
                                            Common.LINK_ID,
                                            _tvShowsLink.LinkID))
            };

            if (!string.IsNullOrEmpty(_tvShowsLink.TVShow.ImageUrl))
            {
                rssItem.Image = new RssImage
                {
                    Url = new Uri(_tvShowsLink.TVShow.ImageUrl),
                    Title = _tvShowsLink.TVShow.Name
                };
            }
            return rssItem;
        }
    }

    public class ExtractedLinkMenuItem : MenuItem
    {
        private readonly VideoResult _videoResult;
        private readonly string _imageUrl;

        public ExtractedLinkMenuItem(string name, string imageUrl, VideoResult videoResult)
        {
            _videoResult = videoResult;

            Name = name;
            _imageUrl = imageUrl;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = _videoResult.MovieName,
                Description = (_videoResult.VideoDetails != null) ? _videoResult.VideoDetails.Description : string.Empty,
                PubDate = DateTime.Now,
                Link = new Uri(Utils.LoopDecodeUrl(_videoResult.Link))
            };

            if (!string.IsNullOrEmpty(_imageUrl))
            {
                rssItem.Image = new RssImage
                {
                    Url = new Uri(_imageUrl),
                    Title = _videoResult.MovieName
                };
            }
            
            var rssCategory = new RssCategory { Name = "Movie" };
            rssItem.Categories.Add(rssCategory);

            return rssItem;
        }
    }

    public class TVShowMenuItem : MenuItem
    {
        private readonly TVShow _tvShow;

        public TVShowMenuItem(TVShow tvShow, PluginMods pluginMode)
        {
            _tvShow = tvShow;

            Name = _tvShow.Name;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_MOVIE_ID;
        }

        public override RssItem CreateRssItem()
        {
            _tvShow.SeriesReference.Load();

            var rssItem = new RssItem
            {
                Title = _tvShow.Name,
                Description = _tvShow.Series.Plot,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                            Utils.GetSiteUrl(),
                                            Common.PLUGIN_MODE,
                                            (int)PluginMode,
                                            Common.FILTER_TYPE,
                                            (int)FilterType,
                                            Common.VIDEO_ID,
                                            _tvShow.VideoID))
            };

            if (!string.IsNullOrEmpty(_tvShow.ImageUrl))
            {
                rssItem.Image = new RssImage
                {
                    Url = new Uri(_tvShow.ImageUrl),
                    Title = _tvShow.Name
                };
            }
            return rssItem;
        }

    }

    public class SeriesMenuItem : MenuItem
    {
        private readonly Series _series;
        private readonly long _siteId;
        private readonly HebSubFilter _hebSubFilter = HebSubFilter.NO_FILTER;

        public enum HebSubFilter
        {
            NO_FILTER,
            TAKE_HEB_SUB
        }

        public SeriesMenuItem(long siteId, Series series, PluginMods pluginMode)
        {
            _siteId = siteId;
            _series = series;

            Name = _series.Name;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_SERIES_ID;
        }

        public SeriesMenuItem(Series series, HebSubFilter hebSubFilter, PluginMods pluginMode)
        {
            _series = series;
            _hebSubFilter = hebSubFilter;

            Name = _series.Name;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_SERIES_ID;
        }

        public SeriesMenuItem(Series series, PluginMods pluginMode)
        {
            _series = series;

            Name = _series.Name;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_SERIES_ID;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = _series.Name,
                Description = _series.Plot,
                PubDate = DateTime.Now,
                
            };

            if(_siteId != 0)
            {
                rssItem.Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}",
                                                     Utils.GetSiteUrl(),
                                                     Common.PLUGIN_MODE,
                                                     (int)PluginMode,
                                                     Common.FILTER_TYPE,
                                                     (int)FilterType,
                                                     Common.SITE_ID,
                                                     _siteId,
                                                     Common.SERIES_ID,
                                                     _series.SeriesID));
            }
            else if(_hebSubFilter != HebSubFilter.NO_FILTER)
            {
                rssItem.Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}",
                                                     Utils.GetSiteUrl(),
                                                     Common.PLUGIN_MODE,
                                                     (int)PluginMode,
                                                     Common.FILTER_TYPE,
                                                     (int)FilterType,
                                                     Common.HEB_SUB_FILTER,
                                                     (int)_hebSubFilter,
                                                     Common.SERIES_ID,
                                                     _series.SeriesID));
            }
            else
            {
                rssItem.Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                                     Utils.GetSiteUrl(),
                                                     Common.PLUGIN_MODE,
                                                     (int) PluginMode,
                                                     Common.FILTER_TYPE,
                                                     (int) FilterType,
                                                     Common.SERIES_ID,
                                                     _series.SeriesID));
            }


            if (!string.IsNullOrEmpty(_series.ImageUrl))
            {
                rssItem.Image = new RssImage
                {
                    Url = new Uri(_series.ImageUrl),
                    Title = _series.Name
                };
            }
            return rssItem;
        }

    }

    public class SiteMenuItem : MenuItem
    {
        private readonly Site _site;

        public SiteMenuItem(Site site, PluginMods pluginMode)
        {
            _site = site;

            Name = site.SiteName;
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_SITE_ID;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = _site.SiteName,
                Description = _site.SiteURL,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                                    Utils.GetSiteUrl(),
                                                    Common.PLUGIN_MODE,
                                                    (int)PluginMode,
                                                    Common.FILTER_TYPE,
                                                    (int)FilterType,
                                                    Common.SITE_ID,
                                                    _site.SiteID))
            };
            
            return rssItem;
        }

    }

    public class AtoZMenuItem : MenuItem
    {
        private KeyValuePair<int, string> _item;

        public AtoZMenuItem(KeyValuePair<int, string> item, PluginMods pluginMods, FilterTypes filterType)
        {
            this._item = item;
            this.PluginMode = pluginMods;
            this.FilterType = filterType;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = this._item.Value,
                Description = this._item.Value,
                PubDate = DateTime.Now,
                Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}",
                                                    Utils.GetSiteUrl(),
                                                    Common.PLUGIN_MODE,
                                                    (int)PluginMode,
                                                    Common.FILTER_TYPE,
                                                    (int)FilterType,
                                                    Common.A_TO_Z_ID,
                                                    this._item.Key))
            };

            return rssItem;
        }

    }

    public class SeasonsNumberMenuItem : MenuItem
    {
        private readonly long _seasonNumber;
        private readonly long _seriesId;
        private readonly long _siteId;
        private readonly SeriesMenuItem.HebSubFilter _hebSubFilter = SeriesMenuItem.HebSubFilter.NO_FILTER;

        public SeasonsNumberMenuItem(long siteId, long seriesId, long seasonNumber, PluginMods pluginMode)
        {
            _siteId = siteId;
            _seriesId = seriesId;
            _seasonNumber = seasonNumber;

            Name = string.Format("עונה {0}", _seasonNumber);
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_SEASON_NUMBER;
        }

        public SeasonsNumberMenuItem(long seriesId, long seasonNumber, SeriesMenuItem.HebSubFilter hebSubFilter, PluginMods pluginMode)
        {
            _seriesId = seriesId;
            _seasonNumber = seasonNumber;
            _hebSubFilter = hebSubFilter;

            Name = string.Format("עונה {0}", _seasonNumber);
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_SEASON_NUMBER;
        }

        public SeasonsNumberMenuItem(long seriesId, long seasonNumber, PluginMods pluginMode)
        {
            _seriesId = seriesId;
            _seasonNumber = seasonNumber;

            Name = string.Format("עונה {0}", _seasonNumber);
            PluginMode = pluginMode;
            FilterType = FilterTypes.FILTER_TYPES_SEASON_NUMBER;
        }

        public override RssItem CreateRssItem()
        {
            var rssItem = new RssItem
            {
                Title = this.Name,
                Description = this.Name,
                PubDate = DateTime.Now,
                
            };

            if (_siteId != 0)
            {
                rssItem.Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}&{9}={10}",
                                                    Utils.GetSiteUrl(),
                                                    Common.PLUGIN_MODE,
                                                    (int)PluginMode,
                                                    Common.FILTER_TYPE,
                                                    (int)FilterType,
                                                    Common.SITE_ID,
                                                    _siteId,
                                                    Common.SERIES_ID,
                                                    _seriesId,
                                                    Common.SEASON_NUMBER,
                                                    _seasonNumber));
            }
            else if (_hebSubFilter != SeriesMenuItem.HebSubFilter.NO_FILTER)
            {
                rssItem.Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}&{9}={10}",
                                                    Utils.GetSiteUrl(),
                                                    Common.PLUGIN_MODE,
                                                    (int)PluginMode,
                                                    Common.FILTER_TYPE,
                                                    (int)FilterType,
                                                    Common.SERIES_ID,
                                                    _seriesId,
                                                    Common.SEASON_NUMBER,
                                                    _seasonNumber,
                                                    Common.HEB_SUB_FILTER,
                                                    (int)_hebSubFilter));
            }
            else
            {
                rssItem.Link = new Uri(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}",
                                                    Utils.GetSiteUrl(),
                                                    Common.PLUGIN_MODE,
                                                    (int)PluginMode,
                                                    Common.FILTER_TYPE,
                                                    (int)FilterType,
                                                    Common.SERIES_ID,
                                                    _seriesId,
                                                    Common.SEASON_NUMBER,
                                                    _seasonNumber));
            }

            return rssItem;
        }

    }
}